package com.eff.android.fragment.main;

import com.eff.android.R;
import com.eff.android.fragment.EBaseFragment;
import com.eff.android.fragment.IFragmentCallback;
import com.eff.android.fragment.main.start.EMainFragmentStartRecord;
import com.eff.android.fragment.options.EMainFragmentOptions;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import android.view.View.OnClickListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class EMainFragment extends EBaseFragment implements IFragmentCallback{
	
	public static String TAG = EMainFragment.class.getSimpleName();
	private ViewHolder holder;
	
	private Button btnStart;
	private Button btnOptions;
	private Button btnIntro;
	
	private FragmentManager eFragmentManager;
    private FragmentTransaction eFragmentTransaction;
	
	public static EMainFragment newInstance (Context context, Bundle b){
		
		EMainFragment f = new EMainFragment();		
		return f;		
	}
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		holder = ViewHolder.newInstance();
		((EMainFragmentStartRecord) holder.start).setCallback(this);
		((EMainFragmentOptions) holder.options).setCallback(this);
		
		eFragmentManager = getFragmentManager();
		
		Log.d("app name", getActivity().getApplicationContext().getPackageName());
    }
	
	 @Override
	    public View onCreateView(LayoutInflater inflater, ViewGroup container,
		    Bundle savedInstanceState) {
			 super.onCreateView(inflater, container, savedInstanceState);
			 
			 View v = inflater.inflate(R.layout.e_frm_main, container, false);
			 btnStart = (Button)v.findViewById(R.id.btnStart);
			 btnStart.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					gotoNextPage(holder.start);
				}
			 });
			 
			 btnOptions = (Button)v.findViewById(R.id.btnOptions);
			 btnOptions.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Log.d("click options", "Ham click");
					gotoNextPage(holder.options);
				}
			 });
			 
			 btnIntro = (Button)v.findViewById(R.id.btnIntro);
			 btnIntro.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					gotoNextPage(holder.intro);
				}
			 });
		 
			 return v;
	 }
	
	private final static class ViewHolder{
		Fragment start;
		Fragment options;
		Fragment intro;
		
		public static ViewHolder newInstance(){
			ViewHolder holder = new ViewHolder();
			holder.initFragments();
			
			return holder;
		}
		
		public void initFragments(){
			start = new EMainFragmentStartRecord();
			options = new EMainFragmentOptions();
			intro = new EMainFragmentIntroduction();
		}
	}

	@Override
	public void gotoNextPage(Fragment frm) {
		eFragmentTransaction = eFragmentManager.beginTransaction();
		eFragmentTransaction.replace(R.id.contentFrame, frm);
		eFragmentTransaction.addToBackStack(null);
	 	eFragmentTransaction.commit();
	}

}
